import wx from 'weixin-js-sdk';
import http from '../../util/httpUtil'
import cookies from 'vue-cookies'
import {Notify} from 'vant';

export default {
  //微信jssdk初始化
  startJSSDK(Vue, fun) {
    //设置全局jssdk
    Vue.prototype.$wechat = wx
    // let authUrl = window.location.href.split("?")[0];
    http.myGet('/wxSystem/getConfigByUrl?url=' + encodeURIComponent(window.location.href)).then(response => {
      Vue.prototype.$wechat.config({
        debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
        appId: response.data.appId, // 必填，公众号的唯一标识
        timestamp: response.data.timestamp, // 必填，生成签名的时间戳
        nonceStr: response.data.nonceStr, // 必填，生成签名的随机串
        signature: response.data.signature,// 必填，签名
        jsApiList: [
          'scanQRCode',
          'chooseImage',
          'uploadImage',
          'configWXDeviceWiFi',
          'openWXDeviceLib',
          'startScanWXDevice',
          'onScanWXDeviceResult',
          'getLocation',
          "onMenuShareTimeline",
          "onMenuShareAppMessage",
          "onMenuShareQQ",
          "onMenuShareWeibo",
          "onMenuShareQZone"
        ] // 必填，需要使用的JS接口列表
      });
      Vue.prototype.$wechat.ready(() => {
        fun.call(this);
      })
      fun.call(this, 123);
    }).catch(error => {
      console.log(error);
    });
  },
  initHttp(Vue, fun) {
    Vue.prototype.$http = http;
    fun.call(this);
  },
  setNotifyType(msg) {
    return {
      suc: {
        message: msg,
        color: '',
        background: '',
        type: 'success'
      },
      err: {
        message: msg,
        color: '#ad0000',
        background: '#ffe1e1',
      }
    }
  },
  initNotify(Vue) {
    let that = this;
    Vue.use(Notify);
    Vue.prototype.$setNT = that.setNotifyType;
    Notify.setDefaultOptions(Vue.prototype.$setNT('').err);
  },
  initCookies(Vue) {
    Vue.prototype.$cookies = cookies
    // Vue.prototype.$cookies.config(30, '/')  // default: expireTimes = 1d , path=/
  },
  //未登录重定向到登录页面
  checkLogin(Vue) {
    //存在open Cookie
    let cook = Vue.prototype.$cookies.get(process.env.VUE_APP_COOKIE_USER_KEY);
    // alert(JSON.stringify(cook))
    if (cook) {
      if (cook['userInfo'] && cook['userInfo']['phone']) {
        return;
      }
    }
    //不存在用户登录信息
    let bashUrl = window.location.href.split("?")[0];
    let baseUrlArr = bashUrl.split("/");
    let uri = baseUrlArr[baseUrlArr.length - 1];
    //忘记密码和登录不校验cookie
    if (uri === "login.html" || uri === "forgetpasswd.html" || uri === "register.html") {
      return;
    }
    window.location.href = "login.html";
  },
  setHTMLFontSise() {
    let windowWidth = document.documentElement.clientWidth;
    //判断视口宽度是不是已经大于了最大版心
    if (windowWidth > 540) {
      document.documentElement.style.fontSize = "20px";
    } else if (windowWidth < 320) {
      //判断视口宽度是不是已经小于了最大版心
      document.documentElement.style.fontSize = 320 / 540 * 20 + "px";
    } else {
      document.documentElement.style.fontSize = windowWidth / 540 * 20 + "px";
    }
  },
  initCommon(Vue, fun) {
    let that = this;
    that.initNotify(Vue);
    that.initCookies(Vue);
    that.checkLogin(Vue);
    Vue.prototype.$setHTMLFontSise = that.setHTMLFontSise;
    Vue.prototype.$getFontSize = that.getFontSize;
    fun.call(this);
  }
}
